package com.example.work4.fragment

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.os.Message
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.work4.R
import com.example.work4.adapter.MomentsAdapter
import com.example.work4.adapter.RecyclerLine
import com.example.work4.common.FIN
import com.example.work4.data.MomentsUtil
import com.example.work4.http.MomentRetrofitImp
import com.example.work4.http.body.StaticRequestBody
import com.example.work4.http.data.MomentPageData
import com.example.work4.po.Moments
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [ListFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class ListFragment : Fragment() {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null

    var mHandler= @SuppressLint("HandlerLeak")
    object :Handler()
    {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when(msg.what)
            {
                FIN.MESSAGEOFMOMENTS->
                {
                    var momentPageData:MomentPageData = msg.obj as MomentPageData
                    updateListView(momentPageData.items)

                }

                FIN.OPENBOARDKEY ->{
                    val inputMethodManager: InputMethodManager =
                        context!!.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                    inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS)
                }
            }
        }

    }

    var apiMoment = MomentRetrofitImp()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    var momentsListView:RecyclerView? = null

    var adapter:MomentsAdapter? = null

    override fun onResume() {
        super.onResume()
        init()
    }

    fun updateListView(list:List<Moments>?)
    {
        adapter?.refreshData(list as ArrayList<Moments>)
    }

    var mRefreshLayout: SmartRefreshLayout? = null

    fun init()
    {
        mRefreshLayout = view?.findViewById(R.id.srl)
        mRefreshLayout!!.setOnRefreshListener {
            apiMoment.getAllFriendsMoments(StaticRequestBody.getStaticRequestBody().momentSelectBody)
            mRefreshLayout!!.finishRefresh(2000)
        }
        apiMoment.context = context;
        apiMoment.handler=mHandler
        MomentsUtil.init(context)
        momentsListView=view?.findViewById(R.id.momentRecyclerView)
        momentsListView?.layoutManager= LinearLayoutManager(context)
        adapter= context?.let { MomentsAdapter(it) }
        adapter?.data= ArrayList()
        momentsListView?.adapter=adapter
        momentsListView?.addItemDecoration(RecyclerLine(3));
        StaticRequestBody.getStaticRequestBody().momentSelectBody.resetCurId() ;
        apiMoment.getAllFriendsMoments(StaticRequestBody.getStaticRequestBody().momentSelectBody)
        FIN.ListFragmentHandler = mHandler
    }





    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_list, container, false)
    }

    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment ListFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            ListFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }

}